<template>
  <div class="login">
    <div class="container">
      <div class="form-container">
        <h2>用户登录</h2>
        <form @submit.prevent="login">
          <div class="form-group">
            <label for="username">用户名或邮箱:</label>
            <input
                type="text"
                id="username"
                v-model="username"
                required
                :class="{ 'error': errors.username }">
            <div v-if="errors.username" class="error-message">{{ errors.username }}</div>
          </div>

          <div class="form-group">
            <label for="password">密码:</label>
            <input
                type="password"
                id="password"
                v-model="password"
                required
                :class="{ 'error': errors.password }">
            <div v-if="errors.password" class="error-message">{{ errors.password }}</div>
          </div>

          <div v-if="errors.general" class="error-message general-error">{{ errors.general }}</div>

          <button type="submit" class="btn btn-primary">登录</button>
        </form>

        <div class="form-footer">
          <p>还没有账户?
            <router-link to="/register">立即注册</router-link>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import apiService from '@/services/api'

export default {
  name: 'Login',
  data() {
    return {
      username: '',
      password: '',
      errors: {
        username: '',
        password: '',
        general: ''
      }
    }
  },
  methods: {
    validateForm() {
      // 重置错误信息
      this.errors = {
        username: '',
        password: '',
        general: ''
      };

      let isValid = true;

      // 验证用户名
      if (!this.username) {
        this.errors.username = '请输入用户名或邮箱';
        isValid = false;
      } else if (this.username.length < 3) {
        this.errors.username = '用户名至少需要3个字符';
        isValid = false;
      }

      // 验证密码
      if (!this.password) {
        this.errors.password = '请输入密码';
        isValid = false;
      } else if (this.password.length < 6) {
        this.errors.password = '密码至少需要6个字符';
        isValid = false;
      }

      return isValid;
    },

    login() {
      // 表单验证
      if (!this.validateForm()) {
        return;
      }

      // 登录请求
      this.simulateLogin();
    },

    async simulateLogin() {
      const params = {username: this.username, password: this.password}
      const response = await apiService.login(params);
      debugger
      console.log(response)
      if (response.success) {
        // 登录成功
        this.handleLoginSuccess(response.data);
      } else {
        // 错误通知
        this.$notify.error(`${response.error}`)
      }
    },

    handleLoginSuccess(data) {
      // 设置登录状态
      localStorage.setItem('isLoggedIn', 'true');
      localStorage.setItem('loginInfo', JSON.stringify(data));
      // 更新 apiService 中的 token
      if (data.token) {
        apiService.setToken(data.token);
        // apiService.setToken("32623");
      }
      setTimeout(() => {
        // 跳转到首页
        this.$router.push('/');
      }, 1000);
    }
  }
}
</script>

<style scoped>
.login {
  min-height: 100vh;
  background: linear-gradient(to bottom, #0c1a2d, #1a3a5f);
  color: #fff;
  padding: 50px 0;
  display: flex;
  align-items: center;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.form-container {
  max-width: 600px;
  width: 100%;
  margin: 0 auto;
  background: rgba(255, 255, 255, 0.1);
  padding: 50px;
  border-radius: 15px;
  backdrop-filter: blur(10px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}

h2 {
  text-align: center;
  font-size: 2.5rem;
  margin-bottom: 40px;
  color: #ff6b35;
}

.form-group {
  margin-bottom: 25px;
  text-align: left;
}

label {
  display: block;
  margin-bottom: 8px;
  font-weight: bold;
  font-size: 1.2rem;
}

input {
  width: 100%;
  padding: 15px;
  border-radius: 8px;
  border: 2px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.9);
  font-size: 1.1rem;
  transition: border-color 0.3s ease;
  box-sizing: border-box;
}

input:focus {
  outline: none;
  border-color: #ff6b35;
}

input.error {
  border-color: #e63946;
}

.error-message {
  color: #e63946;
  font-size: 0.9rem;
  margin-top: 5px;
  text-align: left;
}

.general-error {
  background: rgba(230, 57, 70, 0.2);
  padding: 15px;
  border-radius: 8px;
  margin-bottom: 20px;
}

.btn {
  display: inline-block;
  padding: 15px 40px;
  border-radius: 30px;
  text-decoration: none;
  font-weight: bold;
  transition: all 0.3s ease;
  border: 2px solid;
  cursor: pointer;
  width: 100%;
  font-size: 1.3rem;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.btn-primary {
  background-color: #e63946;
  color: white;
  border-color: #e63946;
  box-shadow: 0 5px 15px rgba(230, 57, 70, 0.4);
}

.btn-primary:hover {
  background-color: transparent;
  color: #e63946;
  transform: translateY(-3px);
  box-shadow: 0 8px 20px rgba(230, 57, 70, 0.6);
}

.btn:disabled {
  background-color: #6c757d;
  border-color: #6c757d;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.form-footer {
  margin-top: 30px;
  text-align: center;
  font-size: 1.1rem;
}

a {
  color: #ff9e00;
  text-decoration: none;
  font-weight: bold;
}

a:hover {
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .form-container {
    padding: 30px 20px;
    margin: 0 15px;
  }

  h2 {
    font-size: 2rem;
  }

  label {
    font-size: 1rem;
  }

  input {
    padding: 12px;
    font-size: 1rem;
  }

  .btn {
    padding: 12px 30px;
    font-size: 1.1rem;
  }
}
</style>